﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure;
using Microsoft.Practices.Prism.Commands;
using System.Windows;
using Microsoft.Practices.Prism.Events;
using Infrastructure.Events;
using Microsoft.Practices.Unity;
using DBDataAccessLib;
using DBDataAccessLib.DataMapping;
using PluginManager;

namespace ALPShell.ViewModel
{
    public class LogonWindowViewModel : ViewModelBase
    {
        private string userName = "admin";

        public string UserName
        {
            get { return userName; }
            set
            {
                userName = string.IsNullOrWhiteSpace(value) ? value : value.Trim();
                OnPropertyChanged("UserName");
                LogonCommand.RaiseCanExecuteChanged();
            }
        }

        private string password = "admin";

        public string Password
        {
            get { return password; }
            set
            {
                password = string.IsNullOrWhiteSpace(value) ? value : value.Trim();
                OnPropertyChanged("Password");
                LogonCommand.RaiseCanExecuteChanged();
            }
        }

        public bool IsSwitchUser { get; set; }

        public DelegateCommand<object> LogonCommand { get; private set; }
        public DelegateCommand<object> CancelLogonCommand { get; private set; }

        public LogonWindowViewModel(
            IUnityContainer container,
            IEventAggregator eventAggregator)
            : base(container, eventAggregator)
        {
            this.LogonCommand = new DelegateCommand<object>(OnLogonCommand, OnCanLogonCommand);
            this.CancelLogonCommand = new DelegateCommand<object>(OnCancelLogonCommand);
        }

        public void OnLogonCommand(object obj)
        {
            AccountMapping account = UserDataAccess.SelectUserNameAndPasswordCount(UserName, Password);
            if (account == null)
            {
                MessageBox.Show("用户名或者密码不正确!");
                return;
            }
            else
            {
                if (GeneralInformation.ViewStates.ContainsKey("UserInfo"))
                    GeneralInformation.ViewStates["UserInfo"] = account;
                else GeneralInformation.ViewStates.Add("UserInfo", account);
            }
            if (!IsSwitchUser) base.EventAggregator.GetEvent<BeginInitializeModulesEvent>().Publish(null);
            else
            {
                EventAggregator.GetEvent<OnSwitchUserEvent>().Publish(null);
                EventAggregator.GetEvent<KillAppEvent>().Publish(new KillAppEventPayload { IsKeptAlive = true });
            }
        }

        public bool OnCanLogonCommand(object obj)
        {
            if (!string.IsNullOrWhiteSpace(this.UserName) && !string.IsNullOrWhiteSpace(Password))
                return true;
            else return false;
        }

        public void OnCancelLogonCommand(object obj)
        {
            if (!this.IsSwitchUser) Application.Current.Shutdown(99);
            else EventAggregator.GetEvent<KillAppEvent>().Publish(new KillAppEventPayload { IsKeptAlive = true });
        }
    }
}
